package com.mxprojects.views
{
	import com.mxprojects.gui.BreadCrumbs;
	import com.mxprojects.gui.DefaultDialog;
	import com.mxprojects.gui.Resizer;
	import com.mxprojects.io.BoardCookie;
	import com.mxprojects.io.Config;
	import com.mxprojects.io.CustomEvent;
	import com.mxprojects.io.DeepLinking;
	import com.mxprojects.io.ServerHandler;
	import com.mxprojects.io.UserControl;
	import com.mxprojects.language.Language;
	import com.mxprojects.language.Word;
	
	import flash.events.MouseEvent;
	
	public class Profile extends Register
	{		
		
		private static var serverHandler:ServerHandler = new ServerHandler();
		
		public function Profile(){	}
		
		public static function load():void {
			//make sure there is a logged in user, otherwise send them back to the main forum
			if(!UserControl.loggedIn){
				DefaultDialog.show(Language.main(Word.ERROR_NOT_LOGGED_IN), true, Word.ERROR, permissionDenied);				
				return;
			}
			
			//set the action to what it should be
			action = ACTION_LOAD_USER;
			
			var dataToSend:Object = new Object();
			dataToSend.action = action;
			dataToSend.userID = BoardCookie.getUserID();
			dataToSend.avatarDir = Config.INSTALL_DIRECTORY + Config.IMAGE_DIRECTORY + Config.AVATAR_UPLOAD_SUB_DIRECTORY;
			
			//clear the stage			
			MXP3.clearBoard();
			
			//resize stuff real quick
			Resizer.resizeContent();
			
			serverHandler.transmitData("profile.php", dataToSend);
			serverHandler.listenForData(showProfile);
		}
		
		private static function showProfile(event:CustomEvent):void {
			//tell the board where we are
			MXP3.currentLocation = Word.PROFILE;
			BreadCrumbs.createCrumbs(0, "", 0, "", Word.PROFILE, Language.main(Word.PROFILE));
			DeepLinking.setTitle(Language.main(Word.PROFILE));
			
			//snag the data
			var profileData:Object = event.getServerData();

			//create a holder to hold all of the input boxes
			clearHolder();
			
			//build up what the input fields should be
			defineInputs(true);
			
			//see how many extra form fields we have
			defineExtraInputs(profileData);
			
			//show everything
			displayProfile(profileData);
			
			//set the action to what it should be
			action = ACTION_UPDATE_USER;			
		
			//at it to the stage
			MXP3.addContent(holder);
			MXP3.addContent(registerButton);
		
			//resize if we need to
			resize();
		}
		
		private static function displayProfile(profileData:Object):void {			
			//create the title to show where we are
			createTitle(Language.main(Word.EDIT_PROFILE));
			
			//build the input boxes
			buildInputBoxes(profileData);
			
			//toss the legend on here
			createLegend();
			
			//add the thing to upload an avatar
			if(profileData.canEditAvatar == 1){
				//show them the uploader if they are allowed to change it				
				createAvatar(profileData.avatarURL);				
			}
			
			//put the divider and button in place
			createDividerAndButton(Language.main(Word.UPDATE));
		}
		
		private static function permissionDenied():void {
			//this is just incase they try to go to the profile page without being logged in
			DeepLinking.setLink(Word.FORUMS, DeepLinking.createLinkArray());
		}
	}
}